<?php


namespace App\Http\Controllers\Admin\User;

use App\Http\Controllers\Controller;
use App\Exceptions\LogicException;
use App\Models\Adminer\Admins;
use App\Models\Adminer\Role;
use App\Services\ValidatorService;
use App\Utils\QueryUtil;
use App\Utils\ResUtil;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class AdminController extends Controller
{
    public function admins()
    {
        $request = request()->all();
        $query = Admins::where(function($query) use($request) {
            QueryUtil::selectBuild($query, $request, [
                //['mobile', '='],
            ]);
            if(!empty($request['name'] ?? '')) {
                $query->where('name', 'like', '%'. $request['name'].'%')->orWhere('phone', 'like', '%'. $request['name'].'%');
            }

            if(!empty($request['phone'] ?? '')) {
                $query->where('phone', 'like', '%'. $request['phone'].'%');
            }
        });

        $total = $query->count();
        $models = QueryUtil::queryList($query);
        $list = [];
        foreach($models as $i => $v) {
            $list[$i] = $v->toArray();
            $list[$i]['role_name'] = Role::find($v->role_id)->name ?? '';
        }
        $hasMore = QueryUtil::hasMore(sizeof($list));
        return $this->ok(compact('list', 'total', 'hasMore'));
    }


    public function addAdmin()
    {
        $data = request()->only(
            'id', 'phone', 'name', 'role_id', 'remark'
        );
        $rule = [
            //'id' => '',
            'phone' => 'required',
            'name' => 'string|required',
            'role_id' => 'numeric|required',
            'remark' => 'string|required'
        ];
        $validator = validatorService()->checkValidator($rule, $data);
        if ($validator['code'] != 200) {
            return $this->error($validator['data']);
        }
        $user = Admins::find($data['id'] ?? '');
        if(!$user) {
            $user = new Admins();
        }

        if(empty($user->password ?? '')) {
            $user->password = sha1(md5((string) $data['phone']));
        }

        $user->name = $data['name'];
        $user->phone = $data['phone'];
        $user->role_id = $data['role_id'];
        $user->remark = $data['remark'];
        $user->save();
        return $this->ok();
    }

    public function delAdmin()
    {
        $data = request()->all();
        $rule = [
            'id' => 'required',
        ];
        $validator = validatorService()->checkValidator($rule, $data);
        if ($validator['code'] != 200) {
            throw new LogicException($validator['data']);
        }

        $user = Admins::find($data['id'] ?? '');
        if(!$user) {
            throw new LogicException('用户不存在');
        }

        $user->enable = 0;
        $user->save();
        return $this->ok();
    }

    public function userInfo()
    {
        $Adminer = self::$authAdmin;
        return $this->ok($Adminer);
    }

    public function resetPwd()
    {
        $data = request()->all();
        $rule = [
            're_password' => 'string|required',
            'password' => 'string|required'
        ];
        $validator = validatorService()->checkValidator($rule, $data);
        if ($validator['code'] != 200) {
            throw new LogicException($validator['data']);
        }

        $user = self::$authAdmin;
        if(!$user) {
            throw new LogicException('用户不存在,请重新登录');
        }
        if($user->password !== sha1($data['password'])) {
            throw new LogicException('原密码不正确');
        }

        $user->password = sha1($data['re_password']);
        $user->save();
        return $this->ok();
    }

}
